<template>
  <div class='ele-body ele-body-card'>
    <a-card
      title="参数设置"
      :bordered='true'
      :body-style="{padding: '20px'}">
      <a-form
        ref='gatewaytimeformRef'
        :model='gatewaytimeformModel'
        :rules="gatewaytimeRules"
        :label-col='{ md: { span: 16 }, sm: { span: 24 } }'
        :wrapper-col='{ md: { span: 8 }, sm: { span: 24 } }'
        @submit='pushHeartInterval'
      >
        <a-alert message="注意：上报时间间隔/上报次数,写0就是不上报,大于0会上报" type="warning" />
        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='主动上报时间间隔(秒0-3600000):' name='reportTime'>
              <a-input v-model:value='gatewaytimeformModel.reportTime' placeholder='主动上报时间间隔' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='报警上报时间间隔(秒0-3600):' name='warnReportTime'>
              <a-input v-model:value='gatewaytimeformModel.warnReportTime' placeholder='报警上报时间间隔' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='采样时间间隔(秒0-360000):' name='collectTime'>
              <a-input v-model:value='gatewaytimeformModel.collectTime' placeholder='采样时间间隔' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='报警上报次数(0-24):' name='warnReportTimes'>
              <a-input v-model:value='gatewaytimeformModel.warnReportTimes' placeholder='报警上报次数' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>


        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='水位上限(精确到1mm):' name='posHigh'>
              <a-input v-model:value='gatewaytimeformModel.posHigh' placeholder='水位上限,范围0-1000000mm' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='水位下限(精确到1mm):' name='posLow'>
              <a-input v-model:value='gatewaytimeformModel.posLow' placeholder='水位下限,范围0-1000000mm' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='水位迟滞(精确到1mm):' name='posDelay'>
              <a-input v-model:value='gatewaytimeformModel.posDelay' placeholder='水位迟滞,范围0-1000000mm' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='16' :sm='24' :xs='24'>
            <a-form-item label='水位变化(精确到1mm):' name='posChange'>
              <a-input v-model:value='gatewaytimeformModel.posChange' placeholder='水位变化,范围0-1000000mm' allow-clear />
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24' :xs='24' />
        </a-row>

        <a-row :gutter='8'>
          <a-col :md='12' :sm='24' :xs='24' />
          <a-col :md='2' :sm='24' :xs='24'>
            <a-button type='primary' html-type='submit' :loading='gatewaytimeformRefLoading'>
              提交
            </a-button>
          </a-col>
          <a-col :md='2' :sm='24' :xs='24'>
            <a-button type='primary' html-type='button' @click="readParam" :loading='readLoading'>
              参数读取
            </a-button>
          </a-col>
          <a-col :md='4' :sm='24' :xs='24' />
        </a-row>
      </a-form>
    </a-card>
  </div>
</template>

<script>
import {DeviceLuoxunWaterPosConfigApi} from "@/api/property/DeviceLuoxunWaterPosConfigApi";
import {DeviceCommandDownQueueApi} from '@/api/property/DeviceCommandDownQueueApi';
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceLuoxunwaterposConfigOther',
  components: {
  },
  props: {
    data: Object
  },
  async created() {
    await this.init();
  },
  data() {
    return {
      loading : false,
      posConfig : {},

      gatewaytimeformModel : {},
      gatewaytimeRules : {},
      gatewaytimeformRefLoading : false,
      gatewaytimeformRefTimer : undefined,

      readLoading : false,
      readRefTimer : undefined
    };
  },
  methods: {
    getDetail() {
      let that = this;
      DeviceLuoxunWaterPosConfigApi.detail({"recordId" : this.data.recordId}).then(result => {
        that.posConfig = result || {};
        this.gatewaytimeformModel = that.posConfig;
      });
    },
    async init() {
      this.getDetail();
    },

    clearGatewaytimeformRef(){
      this.gatewaytimeformRefLoading = false;
      clearInterval(this.gatewaytimeformRefTimer);
    },

    checkProcess(dictate, clearCallback, callback) {
      let params = {
        recordId: this.data.recordId,
        sceneDictate: dictate
      };
      let result = DeviceCommandDownQueueApi.workingDetail(params);
      result
        .then(result => {
          if (!result || !result.dcdqId) { //处理完成了
            clearCallback && clearCallback(); //去掉菊花+清除循环调用
            callback && callback(); //用户自定义
          }
        })
        .catch(() => {
          clearCallback && clearCallback(); //去掉菊花+清除循环调用
        });
    },

    checkHeartInterval(){
      let that = this;
      this.checkProcess(2, this.clearGatewaytimeformRef, function(){
        that.init();
      });
    },
    pushHeartInterval() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 2
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          console.log(workResult);
          if (!workResult || !workResult.dcdqId) { //处理完成了
            that.pushHeartIntervalSubmit();
          } else {
            message.error('上一个配置还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },

    pushHeartIntervalSubmit() {
      this.loading = true;
      let updateDataRecord = {
        recordId: this.data.recordId,
        templateCode: 'waterpos',
        updateData: JSON.stringify({ old: this.posConfig, update: this.gatewaytimeformModel}),
        dictate : 2,
        sceneDictate : 2
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);

          this.gatewaytimeformRefLoading = true; //转圈
          this.gatewaytimeformRefTimer = setInterval(this.checkHeartInterval, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    },
   //==========================读取===============================

    clearReadParamsRef(){
      this.readLoading = false;
      clearInterval(this.readRefTimer);
    },

    checkReadParam(){
      this.checkProcess(3, this.clearReadParamsRef, function(){});
    },

    readParam() {
      let workerDetailparams = {
        recordId: this.data.recordId,
        sceneDictate: 3
      };
      let that = this;
      let existWorkerResult = DeviceCommandDownQueueApi.workingDetail(workerDetailparams);
      existWorkerResult
        .then(workResult => {
          console.log(workResult);
          if (!workResult || !workResult.dcdqId) { //处理完成了
            that.readParamSubmit();
          } else {
            message.error('上一个读取还在推送中，请稍后重试');
            return;
          }
        })
        .catch((e) => {
          message.error(e);
        });
    },
    readParamSubmit() {
      this.loading = true;
      let updateDataRecord = {
        recordId: this.data.recordId,
        templateCode: 'waterpos',
        updateData: JSON.stringify({ old: '', update: ''}),
        dictate : 3,
        sceneDictate : 3
      };
      let result = DeviceCommandDownQueueApi.add(updateDataRecord);
      result
        .then(result => {
          this.loading = false;
          message.success(result.message);
          this.readLoading = true; //转圈
          this.readRefTimer = setInterval(this.checkReadParam, 5000);
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style scoped>
/** 用户卡片 */
.workplace-user-card .ele-cell-content {
  overflow: hidden;
}

.workplace-user-card h4 {
  margin-bottom: 6px;
}
</style>
